﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace studentmanagementdemo
{
    public partial class frmprojectthem : Form
    {

        private SqlConnection con;
        private DataTable dtProject = new DataTable("tblProject");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;

        private void connect()
        {
            //String cn = "Data Source=127.0.0.1;Initial Catalog=iPMAC;Persist Security Info=True;User ID=sa;Password=sa";
            String cn = "Data Source=(local);Initial Catalog=iPMAC;Integrated Security=True";
            try
            {
                con = new SqlConnection(cn);
                con.Open(); //Moi ket noi                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối tới cơ sở dữ liệu !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void disconnect()
        {
            con.Close(); //Dong ket noi
            con.Dispose(); //Giai phong tai nguyen
            con = null; //Huy doi tuong
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand(); //Khai bao 1 command
            command.Connection = con; //Ket noi
            command.CommandType = CommandType.Text; //Khai bao kieu command
            command.CommandText = "Select * from tblProject"; //Cau truy van SQL
            da.SelectCommand = command; //Gan command cho da            
            da.Fill(dtProject); //Nap du lieu vao table              
        }

        private bool kiemtra()
        {
            kt = true;
            if (txttendetai.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tên đề tài !", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }

        public frmprojectthem()
        {
            InitializeComponent();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            frmproject _frmproject = new frmproject();
            _frmproject.Show();
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                DataRow row = dtProject.NewRow();
                row["fldName"] = txttendetai.Text;
                row["fldDescription"] = txtdiengiai.Text;
                dtProject.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = con;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"Insert tblProject (fldName, fldDescription) 
                                                       Values(@fldName, @fldDescription)";
                commandInsert.Parameters.Add("@fldName", SqlDbType.NVarChar, 50, "fldName");
                commandInsert.Parameters.Add("@fldDescription", SqlDbType.NVarChar, 4000, "fldDescription");
                da.InsertCommand = commandInsert;
                da.Update(dtProject);
                MessageBox.Show("Bạn đã thêm thành công !", "THÔNG BÁO", MessageBoxButtons.OK);
                Close();
                disconnect();
                Dispose();
                frmproject _frmproject = new frmproject();
                _frmproject.Show();
            }
        }

        private void frmprojectthem_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            txttendetai.Text = "";
            txtdiengiai.Text = "";
        }
    }
}
